<template>
  <div class="questionnaire-list">
    <h1>可填写的问卷列表</h1>
    
    <div v-if="loading" class="loading">正在加载问卷列表...</div>
    <div v-if="error" class="error">{{ error }}</div>
    <div v-if="deleteSuccess" class="success">问卷已成功删除！</div>
    
    <div v-if="!loading && questionnaires.length === 0" class="empty-list">
      暂无可填写的问卷
    </div>
    
    <div v-if="questionnaires.length > 0" class="questionnaire-cards">
      <div 
        v-for="questionnaire in questionnaires" 
        :key="questionnaire.id"
        class="questionnaire-card"
      >
        <h2>{{ questionnaire.title }}</h2>
        <p v-if="questionnaire.description" class="description">{{ questionnaire.description }}</p>
        <p class="question-count">共 {{ questionnaire.questions.length }} 个问题</p>
        <div class="button-group">
          <router-link 
            :to="`/answer/${questionnaire.id}`" 
            class="btn btn-primary"
          >
            填写问卷
          </router-link>
          <router-link 
            :to="`/edit/${questionnaire.id}`" 
            class="btn btn-warning"
          >
            修改问卷
          </router-link>
          <button 
            class="btn btn-danger" 
            @click="confirmDelete(questionnaire)"
          >
            删除问卷
          </button>
        </div>
      </div>
    </div>
    
    <!-- 删除确认对话框 -->
    <div v-if="showDeleteConfirm" class="modal-backdrop">
      <div class="modal-content">
        <h3>确认删除</h3>
        <p>您确定要删除问卷 "{{ selectedQuestionnaire.title }}" 吗？</p>
        <p class="warning">此操作不可撤销，问卷及其所有问题、选项和图片将被永久删除。</p>
        <div class="modal-buttons">
          <button class="btn btn-secondary" @click="cancelDelete">取消</button>
          <button class="btn btn-danger" @click="deleteQuestionnaire" :disabled="deleting">
            {{ deleting ? '删除中...' : '确认删除' }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent, ref, onMounted } from 'vue';

export default defineComponent({
  setup() {
    const questionnaires = ref([]);
    const loading = ref(true);
    const error = ref('');
    const deleteSuccess = ref(false);
    const showDeleteConfirm = ref(false);
    const selectedQuestionnaire = ref({});
    const deleting = ref(false);
    
    const fetchQuestionnaires = async () => {
      loading.value = true;
      error.value = '';
      
      try {
        const response = await fetch('http://localhost:9001/api/questionnaires');
        if (!response.ok) {
          throw new Error('获取问卷列表失败');
        }
        
        const data = await response.json();
        questionnaires.value = data;
      } catch (err) {
        console.error('获取问卷列表时出错:', err);
        error.value = '加载问卷失败: ' + err.message;
      } finally {
        loading.value = false;
      }
    };
    
    const confirmDelete = (questionnaire) => {
      selectedQuestionnaire.value = questionnaire;
      showDeleteConfirm.value = true;
    };
    
    const cancelDelete = () => {
      showDeleteConfirm.value = false;
      selectedQuestionnaire.value = {};
    };
    
    const deleteQuestionnaire = async () => {
      if (!selectedQuestionnaire.value.id) return;
      
      deleting.value = true;
      error.value = '';
      
      try {
        const response = await fetch(`http://localhost:9001/api/questionnaires/${selectedQuestionnaire.value.id}`, {
          method: 'DELETE'
        });
        
        if (!response.ok) {
          throw new Error('删除问卷失败');
        }
        
        // 从列表中移除已删除的问卷
        questionnaires.value = questionnaires.value.filter(q => q.id !== selectedQuestionnaire.value.id);
        
        // 显示成功消息
        deleteSuccess.value = true;
        setTimeout(() => {
          deleteSuccess.value = false;
        }, 3000);
        
        // 关闭确认对话框
        showDeleteConfirm.value = false;
        selectedQuestionnaire.value = {};
      } catch (err) {
        console.error('删除问卷时出错:', err);
        error.value = '删除问卷失败: ' + err.message;
      } finally {
        deleting.value = false;
      }
    };
    
    onMounted(fetchQuestionnaires);
    
    return {
      questionnaires,
      loading,
      error,
      deleteSuccess,
      showDeleteConfirm,
      selectedQuestionnaire,
      deleting,
      confirmDelete,
      cancelDelete,
      deleteQuestionnaire
    };
  }
});
</script>

<style scoped>
.questionnaire-list {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.empty-list {
  text-align: center;
  padding: 40px;
  background-color: #f9f9f9;
  border-radius: 4px;
  margin-top: 20px;
}

.questionnaire-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.questionnaire-card {
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 20px;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.questionnaire-card h2 {
  margin-top: 0;
  font-size: 1.4em;
}

.description {
  color: #666;
  margin-bottom: 15px;
}

.question-count {
  color: #888;
  font-size: 0.9em;
  margin-bottom: 15px;
}

.button-group {
  display: flex;
  gap: 10px;
}

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}

.btn-primary {
  background-color: #4CAF50;
  color: white;
}

.btn-danger {
  background-color: #f44336;
  color: white;
}

.btn-secondary {
  background-color: #9e9e9e;
  color: white;
}

.loading, .error, .success {
  padding: 10px;
  margin-bottom: 15px;
  border-radius: 4px;
}

.loading {
  background-color: #f9f9f9;
}

.error {
  background-color: #ffebee;
  color: #c62828;
}

.success {
  background-color: #e8f5e9;
  color: #2e7d32;
}

/* 模态框样式 */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background-color: white;
  padding: 20px;
  border-radius: 4px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.modal-content h3 {
  margin-top: 0;
}

.modal-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.warning {
  color: #f44336;
  font-weight: bold;
}
</style> 